import java.util.Arrays;

/**
 * @Author ZhangCuirong
 * @Date 2025/8/12 11:28
 * @description:
 */
public class Solution977 {
    public int[] sortedSquares(int[] nums) {
        int[] res = new int[nums.length];
        int left = 0, right = nums.length - 1;
        int index = res.length - 1;
        while (left <= right) {
            if (Math.abs(nums[left]) < Math.abs(nums[right])) {
                res[index--] = nums[right] * nums[right];
                right--;
            } else {
                res[index--] = nums[left] * nums[left];
                left++;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int[] nums = {-4, -1, 0, 3, 10};
        Solution977 solution = new Solution977();
        int[] res = solution.sortedSquares(nums);
        System.out.println(Arrays.toString(res));
    }
}
